package com.okami.controller;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

/**
 * @author: TanJingyu
 * @create: 2020-12-17 17:07
 **/
@RestController
@RequestMapping("/executor/spring")
public class SpringExecutorController {
    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    public SpringExecutorController(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }

    @GetMapping
    public String executor() {
        System.out.println("池子里大小: " + threadPoolTaskExecutor.getPoolSize());
        System.out.println("干活的线程: " + threadPoolTaskExecutor.getActiveCount());
        return "success";
    }

    @GetMapping("/execut")
    public String execut() throws ExecutionException, InterruptedException {
        Future<?> resultFuture = threadPoolTaskExecutor.submit(() -> {
            try {
                System.out.println("开始睡觉--------" + Thread.currentThread().getName());
                TimeUnit.MINUTES.sleep(30);
                System.out.println("睡醒了............." + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(resultFuture.get());
        return "success";
    }
}
